package nc.bs.arap.busireg;

import nc.bs.ap.poorder.bp.BackBillnoPayableBp;
import nc.bs.businessevent.BdUpdateEvent;
import nc.bs.businessevent.BusinessEvent;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.vo.arap.basebill.BaseAggVO;
import nc.vo.arap.utils.ArrayUtil;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nccloud.commons.lang.ArrayUtils;

public class BackBillnoPayablePG implements IBusinessListener {

	@Override
	public void doAction(IBusinessEvent event) throws BusinessException {
		BaseAggVO[] bills = getBills(event);
		if (ArrayUtils.isEmpty(bills))
			return;
		
		BackBillnoPayableBp bp = new BackBillnoPayableBp();
		
		if("1020".equals(event.getEventType())) {
			bp.order_add();
		}else if("1022".equals(event.getEventType())) {
			bp.order_add_back();
		}
	}

	private BaseAggVO[] getBills(IBusinessEvent event) {
		Object value = null;
		if (event instanceof BusinessEvent) {
			value = ((BusinessEvent) event).getObject();
		} else if (event instanceof BdUpdateEvent) {
			value = ((BdUpdateEvent) event).getNewObject();
		}

		BaseAggVO[] bills = null;
		if (null != value) {
			if (value.getClass().isArray()) {
				Object[] objs = (Object[]) value;
				bills = (objs instanceof BaseAggVO[]) ? (BaseAggVO[]) objs
						: (BaseAggVO[]) ArrayUtil.convertSupers2Subs((AggregatedValueObject[]) objs, BaseAggVO.class);
			} else {
				bills = new BaseAggVO[1];
				bills[0] = (BaseAggVO) value;
			}
		}

		return bills;
	}
}
